<?php

namespace App\Models;

use App\Models\Field\BaseField;
use App\Models\Field\UserField;

/**
 * App\Models\User
 * This is the model class for table "user".
 *
 * @property int $id 自增ID
 * @property string $user_id 用户ID
 * @property int $study_id 学号
 * @property int $industry_id 行业id
 * @property int $industry_parent_id 行业的N级父类ID，按层级以逗号隔开
 * @property int $education_id 教育id
 * @property int $job_id 职业id
 * @property int $job_parent_id 职业的N级父类ID，按层级以逗号隔开
 * @property int $identity 身份：1普通用户，2作者，3游客
 * @property string $phone 用户手机号
 * @property string $password_hash 密码
 * @property string $nickname 昵称
 * @property string $avatar 用户头像
 * @property string $sign 个性签名
 * @property string $description 个人简介
 * @property int $gender 0保密，1男，2女
 * @property int $birthday 出生年份
 * @property int $country 国家 地址表自增 id
 * @property int $province 省 地址表自增 id
 * @property int $city 市 地址表自增 id
 * @property string $address 地址拼接  国家，省，市
 * @property int $status 用户状态，0禁用，1启用
 * @property int $is_robot 是否为机器人，0否，1是
 * @property int $is_deleted 是否删除，0正常，1删除
 * @property int $last_login_at 最后一次登录时间
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User query()
 *
 * @property array $formatLogin
 * @property LoginWeb $loginWeb
 */
class User extends BaseModel
{
    protected $table = "user";
    protected $primaryKey = "id";
    protected $keyType = "string";
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    /**
     * select columns
     * @var array
     */
    public $fields = [
        'article' => ['id', 'user_id', 'nickname', 'avatar'],
        'search'  => ['user_id as id', 'user_id', 'nickname', 'avatar', 'sign'],
        'message' => ['id', 'user_id', 'nickname', 'avatar'],
    ];

    public function getWechatAttribute()
    {
        return AuthWechat::getModel()->select(['id', 'nickname'])->where('user_id', $this->user_id)->first();
    }

    public function getLoginWebAttribute()
    {
        return LoginWeb::getModel()->where('user_id', $this->user_id)->first();
    }

    public function commonFormat($scene = UserField::BASE_INFO): array
    {
        $formats = UserField::getFormats($scene);
        return BaseField::format($this, $formats);
    }

    public function loginWeb()
    {
        return $this->hasOne(LoginWeb::class, 'user_id', 'user_id');
    }
}
